package com.atguigu.spzx.manager.utils;

import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList){
        List<SysMenu> treeList = new ArrayList<>();
        sysMenuList.forEach(menu -> {
            if(menu.getParentId() == 0 ){
                treeList.add(findChildren(menu, sysMenuList));
            }
        });

        return treeList;
    }

    public static SysMenu findChildren(SysMenu menu, List<SysMenu> sysMenuList){
        menu.setChildren(new ArrayList<>());
        // 从sysMenuList中查找menu的子类
        sysMenuList.forEach(cur -> {
            if(cur.getParentId().equals(menu.getId())){
                menu.getChildren().add(findChildren(cur, sysMenuList));
            }
        });

        return menu;

    }
}
